package com.ruoyi.zhmk.service.impl;

import com.ruoyi.common.constant.HttpStatus;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.exception.CustomException;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.zhmk.domain.*;
import com.ruoyi.zhmk.domain.request.PersonCarChartRequest;
import com.ruoyi.zhmk.domain.response.PersonCarChartResponse;
import com.ruoyi.zhmk.domain.response.StatisticsCarResponse;
import com.ruoyi.zhmk.domain.response.StatisticsPersonResponse;
import com.ruoyi.zhmk.mapper.ZhmkCarMapper;
import com.ruoyi.zhmk.mapper.ZhmkCarinoutInfoMapper;
import com.ruoyi.zhmk.mapper.ZhmkPersonMapper;
import com.ruoyi.zhmk.mapper.ZhmkUserinoutInfoMapper;
import com.ruoyi.zhmk.service.IStatisticsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.Month;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author: wwy
 * @Date: 2021/6/28 10:48
 */
@Service
public class StatisticsServiceImpl implements IStatisticsService {
    @Autowired
    private ZhmkPersonMapper zhmkPersonMapper;
    @Autowired
    private ZhmkCarMapper zhmkCarMapper;
    @Autowired
    private ZhmkUserinoutInfoMapper zhmkUserinoutInfoMapper;
    @Autowired
    private ZhmkCarinoutInfoMapper zhmkCarinoutInfoMapper;

    private static List<String> nameList = new ArrayList<>();
    static {
        nameList.add("进门");
        nameList.add("出门");
    }
    /**
     * 首页人员统计
     * 全部人员
     * 请假人员
     * 临时人员
     * @return
     */
    @Override
    public AjaxResult personStatistics() {
        //统计全部人员
        ZhmkPerson zhmkPerson = new ZhmkPerson();
        int personAllCount = zhmkPersonMapper.personStatistics(zhmkPerson);
        //统计请假人员
        ZhmkPerson zhmkPerson1 = new ZhmkPerson();
        zhmkPerson1.setUserStatus(1);
        int askPersonCount = zhmkPersonMapper.personStatistics(zhmkPerson1);
        //统计临时人员
        ZhmkPerson zhmkPerson2 = new ZhmkPerson();
        zhmkPerson2.setUserType(1);
        int temporaryPersonCount = zhmkPersonMapper.personStatistics(zhmkPerson2);
        StatisticsPersonResponse statisticsPersonResponse = new StatisticsPersonResponse();
        statisticsPersonResponse.setPersonAllCount(personAllCount);
        statisticsPersonResponse.setAskPersonCount(askPersonCount);
        statisticsPersonResponse.setTemporaryPersonCount(temporaryPersonCount);
        return AjaxResult.success(statisticsPersonResponse);
    }

    /**
     * 车辆统计
     * 全部车辆
     * 常驻车辆
     * 临时车辆
     * @return
     */
    @Override
    public AjaxResult carStatistics() {
        //全部车辆
        int carAllCount = zhmkCarMapper.carStatistics(new ZhmkCar());
        StatisticsCarResponse statisticsCarResponse = new StatisticsCarResponse();
        statisticsCarResponse.setCarAllCount(carAllCount);
        statisticsCarResponse.setPermanentCarCount(carAllCount);
        return AjaxResult.success(statisticsCarResponse);
    }

    /**
     * 人员车辆统计图
     * @param personCarChartRequest
     * @return
     */
    @Override
    public AjaxResult personCarChart(PersonCarChartRequest personCarChartRequest) {
        if(personCarChartRequest.getType() == null || StringUtils.isEmpty(personCarChartRequest.getDateTime())){
            throw new CustomException("参数错误", HttpStatus.BAD_REQUEST);
        }
        String dateTime = personCarChartRequest.getDateTime();
        String dateDay = dateTime +"-01";
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        LocalDate localDate = LocalDate.parse(dateDay, dateTimeFormatter);
        //该月的天数
        int dayMax = localDate.lengthOfMonth();
        Integer type = personCarChartRequest.getType();

        PersonCarChartResponse personCarChartResponse = new PersonCarChartResponse();
        List<String> dayCountList = new ArrayList<>();
        personCarChartResponse.setDayCount(dayCountList);
        personCarChartResponse.setNameList(nameList);
        List<PersonCarChart> personCarChartList = new ArrayList<>();
        personCarChartResponse.setPersonCarChartList(personCarChartList);

        List<PersonInfoResult> inPersonInfoResultList = null;
        List<PersonInfoResult> outPersonInfoResultList = null;
        if(type.intValue() == 0){
            //人员统计
            //进
            PersonInfoParameter personInfoParameter = new PersonInfoParameter();
            personInfoParameter.setDateTime(dateTime);
            personInfoParameter.setInoutType(0);
            inPersonInfoResultList = zhmkUserinoutInfoMapper.personInfoStatistics(personInfoParameter);
            //出
            personInfoParameter.setInoutType(1);
            outPersonInfoResultList = zhmkUserinoutInfoMapper.personInfoStatistics(personInfoParameter);


        }else if (type.intValue() == 1){
            //车辆统计
            //进
            PersonInfoParameter personInfoParameter = new PersonInfoParameter();
            personInfoParameter.setDateTime(dateTime);
            personInfoParameter.setInoutType(0);
            inPersonInfoResultList = zhmkCarinoutInfoMapper.carInfoStatistics(personInfoParameter);
            //出
            personInfoParameter.setInoutType(1);
            outPersonInfoResultList = zhmkCarinoutInfoMapper.carInfoStatistics(personInfoParameter);

        }else{
            //暂不处理
            inPersonInfoResultList = new ArrayList<>();
            outPersonInfoResultList = new ArrayList<>();
        }

        //进数据
        PersonCarChart personCarChartIn = new PersonCarChart();
        personCarChartIn.setName("进门");
        List<Integer> personCarCharInTotal = new ArrayList<>();
        personCarChartIn.setData(personCarCharInTotal);
        personCarChartList.add(personCarChartIn);
        //出数据
        PersonCarChart personCarChartOut = new PersonCarChart();
        personCarChartOut.setName("出门");
        List<Integer> personCarCharOutTotal = new ArrayList<>();
        personCarChartOut.setData(personCarCharOutTotal);
        personCarChartList.add(personCarChartOut);
        for (int i = 0; i < dayMax; i++) {
            //天数集合
            //判断是否需要加0
            String currentDay = String.valueOf(i+1);
            if(currentDay.length() == 1){
                //前面加0
                currentDay = "0"+currentDay;
            }
            dayCountList.add(currentDay);
            int inTotal = 0;
            int outTotal = 0;
            for (PersonInfoResult personInfoResult : inPersonInfoResultList) {
                if(personInfoResult.getDay().equals(String.valueOf(i+1))){
                    //相等
                    inTotal = personInfoResult.getTotal();
                }
            }

            for (PersonInfoResult personInfoResult : outPersonInfoResultList) {
                if(personInfoResult.getDay().equals(String.valueOf(i+1))){
                    //相等
                    outTotal = personInfoResult.getTotal();
                }
            }
            personCarCharInTotal.add(inTotal);
            personCarCharOutTotal.add(outTotal);

        }
        return AjaxResult.success(personCarChartResponse);
    }
}
